﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Server.Settings
{
    /// <summary>
    /// Class which hold and manipulates the settings for the Thurn and Taxis Server game
    /// </summary>
    public class ServerSettings
    {
        private Boolean _debug;
        private Int32 _portListing;
        private String fileName = "settings.xml";

        // The tags found in the settings xml file
        private const String TAG_ROOT = "settings";
        private const String TAG_DEBUG = "debug";
        private const String TAG_PORT = "port";

        /// <summary>
        /// Creates a new game settings object and load the information from the default file if this exists
        /// </summary>        
        public ServerSettings()
        {
            _debug = false;
            _portListing = 13000;
            LoadServerSettings();
        }

        /// <summary>
        /// Sets or gets the ip 
        /// </summary>
        public Boolean DEBUG
        {
            get
            {
                return _debug;
            }
            set
            {
                _debug = value;
            }
        }

        /// <summary>
        /// Sets or gets the port
        /// </summary>        
        public Int32 Port
        {
            get
            {
                return _portListing;
            }
            set
            {
                _portListing = value;
            }
        }

        /// <summary>
        /// Loads the settings from the default xml file
        /// </summary>      
        public void LoadServerSettings()
        {
            if (File.Exists(fileName))
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(fileName);

                    XmlNodeList nodeList = document.GetElementsByTagName(TAG_DEBUG);
                    if (nodeList.Count != 1) throw new Exception();
                    if (nodeList[0].InnerText.Equals("true") || nodeList[0].InnerText.Equals("1"))
                        _debug = true;
                    else _debug = false;

                    nodeList = document.GetElementsByTagName(TAG_PORT);
                    if (nodeList.Count != 1) throw new Exception();
                    _portListing = Convert.ToInt32(nodeList[0].InnerText);

                }
                catch (Exception)
                {
                    SaveServerSettings();
                }
            }
            else
            {
                SaveServerSettings();
            }
        }
        /// <summary>
        /// Saves the settings to the default xml file
        /// </summary>      
        public void SaveServerSettings()
        {
            XmlDocument document = new XmlDocument();
            XmlElement rootElm = document.CreateElement(TAG_ROOT);

            document.AppendChild(rootElm);

            XmlElement node = document.CreateElement(TAG_DEBUG);
            if (_debug)
                node.InnerText = "true";
            else node.InnerText = "false";

            rootElm.AppendChild(node);
            
            node = document.CreateElement(TAG_PORT);
            node.InnerText = _portListing + "";
            rootElm.AppendChild(node);

            document.Save(fileName);
        }  
    }
}
